package com.su.kddq.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.su.kddq.entity.user.Order;
import com.su.kddq.entity.user.PayOrder;
import com.su.kddq.entity.user.vo.OrderDetail;
import com.su.kddq.service.user.OrderService;
import com.su.kddq.utils.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 订单controller
 *
 * @author sx
 */
@RestController
@RequestMapping("/hddq/express/h5/order/")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户创建订单
     *
     * @param order
     * @return
     */
    @PostMapping({"createOrder"})
    public R createOrder(@RequestBody Order order) {
        Map map = orderService.creatOrder(order);
        boolean isCreat = (Boolean) map.get("isCreat");
        return isCreat ? R.ok().data("orderInfo", order) : R.error().code((Integer) map.get("code"));
    }

    /**
     * 获取订单根据订单状态
     *
     * @param orderState
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping({"getOrderList"})
    public R getOrderList(@RequestParam("orderState") Integer orderState, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        IPage<Order> orderIPage = orderService.getOrderListPage(pageNum, pageSize, orderState);
        return R.ok().data("orderIPage", orderIPage);
    }

    /**
     * 订单详情
     *
     * @param id
     * @return
     */
    @GetMapping({"getOrderDetail/{id}"})
    public R getOrderDetail(@PathVariable String id) {
        OrderDetail orderDetail = orderService.getOrderDetailByid(id);
        return R.ok().data("OrderDetail", orderDetail);
    }


    /**
     * 付款成功异步回调
     *
     * @param request
     * @return
     */
    @PostMapping({"paynotify"})
    public String payNotify(HttpServletRequest request) {
        String resp = orderService.payNotify(request);
        return resp;
    }


    /**
     * 充值成功反馈
     *
     * @param payOrderId
     * @return
     */
    @GetMapping("payResult/{payOrderId}")
    public R payResult(@PathVariable String payOrderId) {
        PayOrder payOrder = orderService.payResult(payOrderId);
        return R.ok().data("payOrder",payOrder);
    }

}
